package graph;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.ParameterizedType;
import org.eclipse.jdt.core.dom.PrimitiveType;
import org.eclipse.jdt.core.dom.Type;

public class TypeResolver {
	
	private TypeResolver() {}

	public static Type getType(AST ast, ITypeBinding typeBinding) {
		if (typeBinding == null) {
			System.out.println("TypeResolver>typeBinding is NULL");
			return null;
		}
		Type returnType = null;
		if (typeBinding.isPrimitive()) {
			System.out.println("TypeResolver>isPrimitive: "+typeBinding.getName());
			returnType = ast.newPrimitiveType(PrimitiveType.toCode(typeBinding.getName()));
			System.out.println("TypeResolver>Primitive type created");
		}
		else {
			System.out.println("TypeResolver>type qualified name:"+typeBinding.getQualifiedName());
			String name = typeBinding.getName().replace("[", "").replace("]", "");
			returnType = ast.newSimpleType(ast.newSimpleName(name));
			System.out.println("TypeResolver>not isPrimitive, created a SimpleType");
		}
		if (typeBinding.isArray()) {
			System.out.println("TypeResolver>isArray");
			returnType = ast.newArrayType(returnType);
		}
		if (typeBinding.isParameterizedType()) {
			System.out.println("TypeResolver>is parameterized type");
			ITypeBinding[] typeArguments = typeBinding.getTypeArguments();
			ParameterizedType newParameterizedType = ast.newParameterizedType(returnType);
			for (ITypeBinding typeArgument:typeArguments)
				newParameterizedType.typeArguments().add(getType(ast, typeArgument));
		}
		return returnType;
	}
	
}
